pub mod common;
pub mod db;
pub mod error;
pub mod models;
pub mod routes;
pub mod schemas;
pub mod handlers;
pub mod services;
pub mod duck;

use crate::error::AppError;
use crate::routes::handler::RouteHandler;
use crate::routes::response::Response;
use routes::map::ROUTES;
use routes::payload::Payload;

#[tauri::command]
async fn router(path: String, payload: Payload) -> Result<Response, String> {
    // 获取路由处理器
    let handler = ROUTES
        .get(path.as_str())
        .ok_or_else(|| AppError::NotFound(path).to_string())?;

    // 异步处理请求
    handler.handle(payload).await.map_err(|e| e.to_string())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![router])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
